<?php

namespace App\Http\Controllers;

use App\Services\WeChatMiniAppService;
use App\Http\Requests\MemberRequest as Request;
use App\Models\Member as Model;
use App\Models\Member;
use App\Models\User;
use App\Notifications\SysNotify;
use Iamxcd\LaravelCRUD\Traits\HasCrud;
use Iamxcd\LaravelCRUD\Traits\HasOption;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;

class MemberController extends Controller
{
    use HasCrud, HasOption;

    protected $app;

    public function __construct(Model $model, WeChatMiniAppService $wx)
    {
        $this->model = $model;
        $this->app = $wx->getApp();
        $this->request = app(Request::class);
        $this->optionLabel = 'nick_name';
    }

    public function updateProfile(Request $request)
    {
        $data = $request->validated();
        $this->model::query()->where('id', Auth::id())->update($data);
        return $this->responseMessage('修改成功');
    }

    public function me()
    {
        $data =  Member::query()
            ->with('defaultCashbook:id,name')
            ->withCount('unreadNotifications as unread_count')
            ->find(Auth::id());
        return $this->response($data);
    }

    public function notifications(Request $request)
    {
        $user = Auth::user();
        $data = $user->notifications()->paginate((int) $request->page_size ?? 10)->toArray();
        return $this->responsePaginate($data['data'], $data['total'], $data['current_page'], $data['per_page']);
    }

    public function notification($id)
    {
        $user = Auth::user();
        $data = $user->notifications()->find($id);
        if (is_null($data)) {
            return $this->responseError('消息不存在');
        }
        $data->markAsRead();
        return $this->response([
            'info' => $data
        ]);
    }

    public function readNotification($id)
    {
        $user = Auth::user();
        $data = $user->notifications()->find($id);
        if (is_null($data)) {
            return $this->responseError('消息不存在');
        }
        $data->markAsRead();
        return $this->responseMessage('操作成功');
    }


    public function delNotification($id)
    {
        $user = Auth::user();
        $data = $user->notifications()->find($id);
        if (is_null($data)) {
            return $this->responseError('消息不存在');
        }
        $data->delete();
        return $this->responseMessage('删除成功');
    }

    public function sendSysNotify()
    {
        $request = $this->request;
        $data = $request->validated();

        if ($data['is_all']) {
            $users = $this->model::query()->get('id');
        } else {
            if (count($data['users']) > 0) {
                $users = $this->model::query()->whereIn('id', $data['users'])->get('id');
            } else {
                return $this->responseError('发送用户不能为空');;
            }
        }
        $notify = new SysNotify($data['title'], $data['content'], $data['sub_title']);
        Notification::send($users, $notify);
        return $this->responseMessage('发送成功');
    }
}
